package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.service.wx.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author：Katherine
 * @Package：com.cskaoyan.market.service.wx.teamwork.impl
 * @Project：market
 * @name：AdminAddressServiceImpl
 * @Date：2023/5/23 16:08
 * @Filename：AdminAddressServiceImpl
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;


    @Override
    public List<MarketAddress> list() {
        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.or().andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(addressExample);
        System.out.println(marketAddresses);
        return marketAddresses;

    }

    @Override
    public MarketAddress detail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        return marketAddress;


    }

    @Override
    public int save(Integer userId, MarketAddress marketAddress) {

        Integer id = marketAddress.getId();
        MarketAddress addr = new MarketAddress();
        addr.setUserId(userId);
        addr.setAddressDetail(marketAddress.getAddressDetail());
        addr.setAreaCode(marketAddress.getAreaCode());
        addr.setCity(marketAddress.getCity());
        addr.setCounty(marketAddress.getCounty());
        addr.setId(marketAddress.getId());
        addr.setIsDefault(marketAddress.getIsDefault());
        addr.setName(marketAddress.getName());
        addr.setProvince(marketAddress.getProvince());
        addr.setTel(marketAddress.getTel());
        addr.setUpdateTime(LocalDateTime.now());
        int row = 0;
        if (id == 0) {
            // 添加地址
            addr.setUpdateTime(LocalDateTime.now());
            addr.setDeleted(false);
            row = marketAddressMapper.insertSelective(addr);
        } else {
            // 修改地址
            row = marketAddressMapper.updateByPrimaryKeySelective(addr);
        }

        return row;
    }

    @Override
    public int delete(Integer id) {
        MarketAddress addr2 = new MarketAddress();
        addr2.setDeleted(true);
        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.or().andIdEqualTo(id).andDeletedEqualTo(false);
        int delete = marketAddressMapper.updateByExampleSelective(addr2, addressExample);
        return delete;

    }
}
